﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarLoanSystem
{
    public partial class panelReportPayment : UserControl
    {
        public panelReportPayment()
       {
            InitializeComponent();
            genGridview();
        }

        private void genGridview()
        {
            // gen Grideview
            gvPayment.AutoGenerateColumns = false;

            //Set Columns Count
            gvPayment.ColumnCount = 7;

            //Add Columns
            gvPayment.Columns[0].Name = "LOAN_ID";
            gvPayment.Columns[0].HeaderText = "รหัส";
            gvPayment.Columns[0].DataPropertyName = "LOAN_ID";
            gvPayment.Columns[0].Width = 50;

            gvPayment.Columns[1].Name = "CUSTOMER_NAME";
            gvPayment.Columns[1].HeaderText = "ชื่อ";
            gvPayment.Columns[1].DataPropertyName = "CUSTOMER_NAME";
            gvPayment.Columns[1].Width = 95;

            gvPayment.Columns[2].Name = "CUSTOMER_LASTNAME";
            gvPayment.Columns[2].HeaderText = "นามสกุล";
            gvPayment.Columns[2].DataPropertyName = "CUSTOMER_LASTNAME";
            gvPayment.Columns[2].Width = 95;

            gvPayment.Columns[3].Name = "PAY_MONTH";
            gvPayment.Columns[3].HeaderText = "ยอดชำระรายเดือน";
            gvPayment.Columns[3].DataPropertyName = "PAY_MONTH";
            gvPayment.Columns[3].Width = 115;

            gvPayment.Columns[4].Name = "DEBT_MONTH_REST";
            gvPayment.Columns[4].HeaderText = "จำนวนเงินที่เหลือ";
            gvPayment.Columns[4].DataPropertyName = "DEBT_MONTH_REST";
            gvPayment.Columns[4].Width = 130;

            gvPayment.Columns[5].Name = "MONTH_QTY_AMOUNT";
            gvPayment.Columns[5].HeaderText = "จำนวนงวดที่ผ่อนแล้ว";
            gvPayment.Columns[5].DataPropertyName = "MONTH_QTY_AMOUNT";
            gvPayment.Columns[5].Width = 130;

            gvPayment.Columns[6].Name = "MONTH_QTY_ALL";
            gvPayment.Columns[6].HeaderText = "จำนวนงวดที่ต้องผ่อนชำระทั้งหมด";
            gvPayment.Columns[6].DataPropertyName = "MONTH_QTY_ALL";
            gvPayment.Columns[6].Width = 135;

            // end gen gridview
            //gvPayment.DataSource = LoadReportPayment();
        }

        private DataTable LoadReportPayment()
        {
            DataTable dt = DAL.ReportInfoDAL.LoadAllPayment(this.txtLoanID.Text, this.txtName.Text, this.txtLastName.Text);
            if (dt != null)
            {
                dt.Columns.Add("MONTH_QTY_AMOUNT", typeof(string));

                foreach (DataRow dr in dt.Rows)
                {
                    if (dr[5].ToString().Equals(""))
                    {
                        dr[7] = "0";
                    }
                    else
                    {
                        dr[7] = dr[5].ToString();
                    }
                }
            }
            return dt;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvPayment.DataSource = LoadReportPayment();
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            ShowReportPayment s = new ShowReportPayment(LoadReportPayment());
            s.ShowDialog();
        }
    }
}